<template>
  <PanerForm :isShowBarDrawer="true" :caption="'编辑资源文件'">
    <template #body>
      <div class="d-row">
        <div class="d-form-group d-form-group-flex">
          <div class="d-form-label d-text-right">
            <label class="label-text">名称</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.name" class="d-form-input" type="text" />
          </div>
        </div>
        <div class="d-row">
          <div class="d-form-group d-form-group-flex">
            <div class="d-form-label d-text-right">
              <label class="label-text">路径</label>
            </div>
            <div class="d-form-el">
              <input v-model="state.path" class="d-form-input" type="text" readonly />
            </div>
          </div>
        </div>
        <div class="d-row">
          <div class="d-form-group d-form-group-flex d-col-xs-6">
            <div class="d-form-label d-text-right">
              <label class="label-text">后缀</label>
            </div>
            <div class="d-form-el">
              <input v-model="state.ext" class="d-form-input" type="text" readonly />
            </div>
          </div>
          <div class="d-form-group d-form-group-flex d-col-xs-6">
            <div class="d-form-label d-text-right">
              <label class="label-text">大小</label>
            </div>
            <div class="d-form-el">
              <input v-model="state.size" class="d-form-input" type="text" readonly />
            </div>
          </div>
          <FormCalendar
            v-model="state.updateTime"
            :isEditAble="false"
            :readonly="true"
            format="yyyy-MM-dd HH:mm:ss"
            label="修改时间"
            labelWidth="90px"
            class="d-form-group-flex d-col-xs-6"
          ></FormCalendar>
          <FormCalendar
            v-model="state.createTime"
            :isEditAble="false"
            :readonly="true"
            format="yyyy-MM-dd HH:mm:ss"
            label="创建时间"
            labelWidth="90px"
            class="d-form-group-flex d-col-xs-6"
          ></FormCalendar>
        </div>
      </div>
    </template>
    <template #bar-drawer>
      <button @click="handleSubmit" class="d-btn d-btn-primary" type="button">提交</button>
      <button v-if="!isEdit" @click="handleReset" class="d-btn d-btn-default" type="reset">重置</button>
      <a v-d-goback class="d-btn d-btn-default">返回</a>
    </template>
  </PanerForm>
</template>

<script>
import { PanerForm } from '@daelui/vdog/dist/pages.js'
import { Dialoger, FormCalendar } from '@daelui/vdog/dist/components.js'
import { zh as locale } from '@daelui/dogjs/dist/locale.js'
import Uploader from '@/components/uploader/uploader'
import pfSer from '@/service/action/pfile.action.js'

export default {
  components: {
    PanerForm,
    FormCalendar,
    Dialoger,
    Uploader
  },

  data () {
    return {
      isEdit: false, // 是否是编辑模式
      // 表单数据
      state: {
        id: '',
        name: '',
        path: '',
        updateTime: '',
        updateUser: '',
        createTime: '',
        createUser: ''
      },
      sourcePath: ''
    }
  },

  methods: {
    /**
     * @function 加载数据
    */
    loadItem (id) {
      pfSer.queryItem({ id }).then(result => {
        let data = result.data || {}
        this.state = data
      })
    },

    /**
     * @function 提交
    */
    handleSubmit () {
      let defer = null
      delete this.state.createTime
      // 编辑
      if (this.isEdit) {
        defer = pfSer.editItem(this.state)
      } else { // 添加
        defer = pfSer.addItem(this.state)
      }
      defer.then(result => {
        if (result.success) {
          Dialoger.success(locale.action.success)
          // 添加则回到列表页
          if (!this.isEdit) {
            this.$router.push(this.getMicroRoute('/pool/list'))
          }
        } else {
          Dialoger.danger(locale.action.fail)
        }
      })
    },

    /**
     * @function 重置
    */
    handleReset () {

    }
  },

  beforeMount () {
    let params = this.$route.query
    // 新建
    if (!params.id) {
      this.isEdit = false
    } else { // 编辑
      this.isEdit = true
      this.loadItem(params.id)
    }
  }
}
</script>

<style scoped>
.d-form-label {
  width: 90px;
}
.d-form-textarea {
  min-height: 120px;
}
</style>
